package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zhangchuang
 * @Date: 2025/09/29 11:00
 * @Description: 部门控制器
 */
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     *
     * @return Result
     */
    @GetMapping("/list")
    public Result getList() {
        List<Dept> list = deptService.getList();
        return Result.success(list);
    }

    /**
     * 分页查询部门
     *
     * @param name     部门名称
     * @param status   部门状态
     * @param page     页码
     * @param pageSize 页大小
     * @return Result
     */
    @GetMapping
    public Result getByPage(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询部门，参数：name={},status={},page={},pageSize={}", name, status, page, pageSize);
        PageResult<Dept> deptPageResult = deptService.listPage(name, status, page, pageSize);
        return Result.success(deptPageResult);
    }

    /**
     * 根据id查询部门
     *
     * @param id 部门id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询部门，参数：id={}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 添加部门
     *
     * @param dept 部门信息
     * @return Result
     */
    @LogRecord
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门，参数：{}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return Result
     */
    @LogRecord
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门，参数：{}", dept);
        deptService.updateById(dept);
        return Result.success();
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return Result
     */
    @LogRecord
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        log.info("删除部门，参数：id={}", id);
        deptService.deleteById(id);
        return Result.success();
    }
}
